<?php

namespace App\Http\Controllers\Develop;

use Carbon\Carbon;

use Illuminate\Http\Request;

use App\Models\ConfigureFunction;

use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Redis as Redis;

class FunctionController extends Controller
{

    private $map = [

        //公共设置
        'configure/common.tpl' => 'configure/common',
        'configure/user.tpl' => 'configure/user',
        'configure/pay.tpl' => 'configure/pay',

        //会员设置
        'configure/customer.tpl' => 'configure/customer',
        'common/configure/customer.tpl' => 'configure/customer/customer',
        'common/configure/editUser.tpl' => 'configure/customer/editUser',
        'common/configure/openCard.tpl' => 'configure/customer/openCard',
        'common/configure/bindCard.tpl' => 'configure/customer/bindCard',
        'common/configure/customerRecharge.tpl' => 'configure/customer/customerRecharge',
        'common/configure/customer/agreement.tpl' => 'configure/customer/agreement',

        //商城设置
        'configure/shop.tpl' => 'configure/shop',
        'configure/sms.tpl' => 'configure/sms',
        'common/configure/cartBtn.tpl' => 'configure/common/cartBtn',//购物车设置
        'common/configure/baidu.tpl' => 'configure/baidu',//百度设置

        //订单设置
        'common/configure/bill.tpl'=> 'configure/order/bill',
        'common/configure/splitBill.tpl'=> 'configure/order/billSplit',
        'common/configure/returnBill.tpl'=> 'configure/order/billReturn',
        'common/configure/billSendTime.tpl'=> 'configure/order/billSendTime',
        'common/configure/billPickTime.tpl'=> 'configure/order/billPickTime',
        'common/configure/pfBillSendTime.tpl'=> 'configure/order/pfBillSendTime',
        'common/configure/bill/cancel.tpl'=> 'configure/order/billCancel',
        'common/configure/bill/rejection.tpl'=> 'configure/order/billRejection',

        //商品设置
        'configure/goods.tpl' => 'configure/goods',
        'configure/store.tpl' => 'configure/store',

        //门店设置
        'configure/mall.tpl' => 'configure/mall',

        //卡券设置
        'configure/card.tpl' => 'configure/card',

        //渠道设置
        'configure/alipaylift.tpl' => 'configure/alipaylift',
        'configure/weixin.tpl' => 'configure/weixin',
        'configure/weixinApplet.tpl' => 'configure/weixinApplet',

    ];

    public function index($function_id)
    {

        $configure_data = ConfigureFunction::getRedis( $function_id );

        $tpl_file = $this->map[$configure_data['tpl_file']];

        return view ('develop/' . $tpl_file, $configure_data);

    }

    //保存设置信息
    public function store(Request $request )
    {

        $project_id = config('system.project_id');
        $id = $request->input('function_id');
        $configure_req = $request->all();
        unset($configure_req['_url']);
        unset($configure_req['function_id']);

        if (!$project_id) {
            return response()->json(['code' => 10000, 'message' => '项目ID不存在']);
        }

        //查询配置信息
        $configure = ConfigureFunction::find($id);
        if ( !$configure ) {
            return response()->json(['code' => 10001, 'message' => '设置不存在']);
        }

        $configure_array = [
            'function_id' => $id,
            'configure' => $configure_req
        ];
        $configure->configure_json = json_encode($configure_array);
        $configure->timeStamp = Carbon::now();
        $configure->save();

        Redis::del('G_CONFIGURE_FUNCTION_DATA_' . $project_id . '_' . $id);

        return response()->json(['code' => 200, 'message' => '保存成功']);

    }

}
